package com.ajx.blog.service.impl;

import com.ajx.blog.entity.Article;
import com.ajx.blog.entity.ArticleComment;
import com.ajx.blog.mapper.ArticleCollInfoMapper;
import com.ajx.blog.mapper.ArticleCommentMapper;
import com.ajx.blog.mapper.ArticleMapper;
import com.ajx.blog.service.api.ArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    private Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCommentMapper articleCommentMapper;

    @Autowired
    private ArticleCollInfoMapper articleCollInfoMapper;

    @Transactional(readOnly = true)
    @Override
    public List<ArticleComment> selectArticle(Integer aid) {
        QueryWrapper<ArticleComment> qw = new QueryWrapper<>();
        qw.eq("blogid", aid);
        return articleCommentMapper.selectList(qw);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean addComment(ArticleComment articleComment) {
        int num = articleCommentMapper.insertComment(articleComment);
        int num1 = articleMapper.updateCommentnum(articleComment.getBlogid());
        if (num > 0 && num1 > 0) {
            return true;
        }
        return false;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int collect(Integer aid, Integer uid) {
        int num = articleMapper.collect(aid);
        // 添加收藏记录
        int num1 = articleCollInfoMapper.addArticleCheckMessage(aid, uid);
        if (num1 > 0 && num > 0) {
            return 1;
        }
        return 0;

    }

    @Transactional(readOnly = true)
    @Override
    public List<Article> selectArticleList(Integer bloguid) {
        QueryWrapper<Article> qw = new QueryWrapper<>();
        qw.eq("bloguid", bloguid).orderByAsc("astatus");
        return articleMapper.selectList(qw);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Article> selectByQW(QueryWrapper<Article> articleQueryWrapper) {
        logger.info("调用了根据条件查询博客的业务");
        return articleMapper.selectList(articleQueryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addArticle(Article article) {
        logger.info("添加博客的业务");
        int i = articleMapper.insertBackKey(article);
        System.out.println(article.getAid());
        return article.getAid();
    }

    @Override
    public PageInfo<Article> selectPgeInfoByQW(QueryWrapper<Article> qw, Integer pageNum, Integer pageSize) {
        logger.info("用户查看自己博客信息的业务");
        PageHelper.startPage(pageNum, pageSize, true);
        List<Article> articles = articleMapper.selectList(qw);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
        return articlePageInfo;
    }

    @Override
    public boolean updateArticle(Article article) {
        logger.info("调用了用户修改博客信息的业务");
        return articleMapper.updateById(article) == 1;
    }

    @Override
    public Article selectbyId(Integer aid) {
        return articleMapper.selectById(aid);
    }

    @Override
    public List<Article> selectByExample(Article article) {
        return articleMapper.selectByExample(article);
    }

    @Override
    public List<Article> selectHottest() {
        logger.info("获取最热文章");
        return articleMapper.selectHottest();
    }

    @Override
    public List<Article> selectByMap(Map<String, Object> stringStringMap) {
        logger.info("根据条件获取文章");
        return articleMapper.selectByMaps(stringStringMap);
    }

    @Override
    public PageInfo<Article> selectPageInfoByEample(Article article, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Article> articles = articleMapper.selectByExample(article);
        return new PageInfo<>(articles);
    }

    /**
     * @param {String} status 文章状态 -1审核不通过 0待审核 1审核通过
     * @return {Integer} 文章数量
     * 
     * @description: 按状态查询文章数量
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(cacheNames = "countByStatus", unless = "#result == null")
    @Override
    public Integer countByStatus(String status) {
        QueryWrapper<Article> articleQW = new QueryWrapper<>();
        articleQW.eq("astatus", status);
        return articleMapper.selectCount(articleQW);
    }

    /**
     * @param {*}
     * @return {Integer} 所有文章数量
     * 
     * @description: 查询所有文章数量
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(cacheNames = "selectCllArticleCount", unless = "#result == null")
    @Override
    public Integer selectCllArticleCount() {
        QueryWrapper<Article> articleQW = new QueryWrapper<>();
        return articleMapper.selectCount(articleQW);
    }

    @Override
    public PageInfo<Article> selectByMapPageInfo(Map<String, Object> stringObjectMap, Integer pageNum,
            Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Article> articles = articleMapper.selectByMaps(stringObjectMap);
        return new PageInfo<>(articles);
    }

    @Override
    public Article selectArticleLists(Integer atricleid) {
        QueryWrapper<Article> qw = new QueryWrapper<>();
        qw.eq("aid", atricleid);
        return articleMapper.selectOne(qw);

    }

    @Override
    public List<Article> selectTime(int i) {
        return null;
    }

    @Override
    public List<Article> selectByExamples(Map<String, Object> stringIntegerMap) {
        return  articleMapper.selectByExamples(stringIntegerMap);
    }


}
